package net.kunyv.base.controller;

import net.kunyv.base.entity.SysUser;
import net.kunyv.base.service.SysUserService;
import net.kunyv.util.HdUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author sunyb
 * @create 2017-08-02 15:57
 */
@Controller
public class LoginController {
    Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(method = RequestMethod.GET, value = "/login")
    public String login() {
        return "login";
    }
    @RequestMapping(method = RequestMethod.GET, value = "/welcome")
    public String welcome() {
        return "login/welcome";
    }
    /**
     * 登录请求
     *
     * @param userName
     * @param password           已加密密码
     * @param rememberMe
     * @param redirectAttributes
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2017/8/2
     */
    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public String login(String userName, String password, String rememberMe, RedirectAttributes redirectAttributes) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            redirectAttributes.addFlashAttribute("errorInfo", "用户名或密码为空");
            return "login";
        }
        UsernamePasswordToken token = new UsernamePasswordToken(userName, HdUtil.md5(password));
        if ("true".equals(rememberMe)) {
            token.setRememberMe(true);
        }
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (UnknownAccountException accountException) {
            logger.error("用户名：" + userName + "登录失败");
            redirectAttributes.addFlashAttribute("errorInfo", "用户名不存在");
        } catch (IncorrectCredentialsException credentialsException) {
            logger.error("用户名：" + userName + "登录失败");
            redirectAttributes.addFlashAttribute("errorInfo", "用户名密码错误");
        } catch (AuthenticationException ex) {
            logger.error("用户名：" + userName + "登录失败");
            redirectAttributes.addFlashAttribute("errorInfo", "用户名密码错误");
        }
        //验证是否登录成功
        if (subject.isAuthenticated()) {
            logger.info(userName + "登录成功");
            //把当前用户放入session
            Session session = subject.getSession();
            SysUser user = sysUserService.getUserByUserName(userName);
            session.setAttribute("loginUser", user);
            return "login/main";
        } else {
            token.clear();
            return "login";
        }
    }
}
